<?php

class PointSaleFormController extends WebController{
    
    private $point_sale = null;
    
    public function processRequest($action_type,  HttpRequest $request,HttpResponse $response) {
        switch ($action_type){
            case "addPointSale":
                $this->addPointSale($request,$response);
                break;
            case "editPointSale":
                $this->editPointSale($request,$response);
                break;
        }
    }

    protected function display(HttpRequest $request,HttpResponse $response) {
        //init country
        $country = new CountryRM();
        $this->orm_builder->buildCountry($country);
        $country->findAllValidCountry();
        
        //Catpcha
        include_once APPLICATION_DIR.'/application/data/captcha.php';
        global $_CAPTCHA;
        $captcha_key = array_keys($_CAPTCHA);
        $captcha_size = count($captcha_key);
        $temp_captcha = $captcha_key[rand(0,$captcha_size-1)];
        $request->setAttribute("temp_captcha",$temp_captcha);
        $request->setAttribute("temp_captcha_url",$_CAPTCHA[$temp_captcha]);

        //Phone code per country
        include_once APPLICATION_DIR.'/application/data/country_phone.php';
        global $_COUNTRY_PHONE;
        $request->setAttribute("country_phone",$_COUNTRY_PHONE);
        
        //Get Point Sale by Id And Check existed
        if($this->point_sale == null && $request->get("id") != null){
            $this->point_sale = new PointSaleRM();
            $this->point_sale->ID = $request->get("id");
            $this->point_sale->map();
            $request->setAttribute("point_sale",$this->point_sale);
        }
        
        //Assign
        $request->setAttribute("country",$country);
        $this->forward("point_sale_form.html",$request);
    }

    protected function addPointSale(HttpRequest $request,HttpResponse $response){
        
        include_once APPLICATION_DIR.'/application/data/captcha.php';
        global $_CAPTCHA;
        $captcha = $request->get("captcha");
        
        //Load point sale information
        $point_sale = new PointSaleRM();
        $point_sale->CITY_ID = $request->getIntNumber("city");
        $point_sale->COUNTRY_ID = $request->getIntNumber("country");
        $point_sale->EMAIL = $request->get("email");
        $point_sale->PHONE = $request->get("phone_code").".".$request->get("phone");      
        $point_sale->OTHER_INFORMATION = $request->get("other_information"); 
        $point_sale->NAME = $request->get("point_sale_name");
        $point_sale->STREET = $request->get("address");
        $point_sale->SUPPLIER_ID = $this->_USER_ID;
        $position = null;
        if($request->get("position") != ""){
            $position = explode(",", $request->get("position"));
            if(count($position) == 2){
                $point_sale->X = $position[0];
                $point_sale->Y = $position[1];
            }
        }
        
        if(!isset($_CAPTCHA[strtolower($captcha)])){
            $this->addError("_MSG_IMAGE_VERIFICATION_NOT_MATCH");
        }else{
            //Create point sale
            $point_sale->createPointSale();
            //Set error for controller to display on view
            $this->setError($point_sale->getErrorMessage()->getValues());
        }
        
        //Check error and redirect if successfully
        if(!$this->hasError()){
            $this->sendRedirect("/point-sale", $response);
        }
        
        //Response information
        $request->setAttribute("point_sale",$point_sale);
        $this->point_sale = $point_sale;
    }
    
    protected function editPointSale(HttpRequest $request,HttpResponse $response){
        
        if($request->get("id") == null){
            $this->sendRedirect("/point-sale", $response);
        }
        
        //Load point sale information
        $point_sale = new PointSaleRM();
        $point_sale->ID = $request->get("id");
        $point_sale->map();
        
        //Check required information is not valid
        if($point_sale->SUPPLIER_ID == null){
            $this->sendRedirect("/point-sale", $response);
        }
        
        $point_sale->CITY_ID = $request->getIntNumber("city");
        $point_sale->COUNTRY_ID = $request->getIntNumber("country");
        $point_sale->EMAIL = $request->get("email");
        $point_sale->PHONE = $request->get("phone_code").".".$request->get("phone");      
        $point_sale->OTHER_INFORMATION = $request->get("other_information"); 
        $point_sale->NAME = $request->get("point_sale_name");
        $point_sale->STREET = $request->get("address");
        $point_sale->SUPPLIER_ID = $this->_USER_ID;
        $position = null;
        if($request->get("position") != ""){
            $position = explode(",", $request->get("position"));
            if(count($position) == 2){
                $point_sale->X = $position[0];
                $point_sale->Y = $position[1];
            }
        }
        
        //Update point sale
        $point_sale->updatePointSale();
        //Set error for controller to display on view
        $this->setError($point_sale->getErrorMessage()->getValues());
       
        
        //Check error and redirect if successfully
        if(!$this->hasError()){
            $this->sendRedirect("/point-sale", $response);
        }
        
        //Response information
        $request->setAttribute("point_sale",$point_sale);
        $this->point_sale = $point_sale;
    }
}
